/*
 * WelfarePart.h
 *
 *  Created on: Nov 29, 2017
 *      Author: gaoyi
 */

#pragma once

#include "Character/Part.h"
#include "Common/DeclareProperty.h"
#include "ProtoBuffer/AllProtocol.h"
#include "Common/OperateLimitDefine.h"
#include "OperateWelfareMgr.h"
#include "Common/Utility/TimeUtility.h"
#include "Common/TableData/ConstantConstantCfg.h"
#include <map>
#include <vector>

class WelfareItem
{
public:
	WelfareItem() { }
	~WelfareItem() { }
public:
	DECLARE_PROPERTY_UINT32(ItemId);		//id, 奖励表的id
	DECLARE_PROPERTY_UINT32(ItemType);		//同活动类型
	DECLARE_PROPERTY_BOOL(IsFinish);		//是否条件达
	DECLARE_PROPERTY_BOOL(IsReceiveReward); //是否领取奖励
public:
	void SaveToPB(WelfareItemInfo& pb)
	{
		pb.set_item_id(mItemId);
		pb.set_item_type(mItemType);
		pb.set_is_finish(mIsFinish);
		pb.set_is_receive_reward(mIsReceiveReward);
	}
	void LoadFromPB(const WelfareItemInfo& pb)
	{
		mItemId = pb.item_id();
		mItemType = pb.item_type();
		mIsFinish = pb.is_finish();
		mIsReceiveReward = pb.is_receive_reward();
	}
};

typedef std::vector<WelfareItem> VecWelfareItem;

class OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(ActId);		// 活动id
	DECLARE_PROPERTY_UINT32(ActType);	//活动类型
public:
	void SaveToPB(OperateActItemInfo& pb)
	{
		pb.set_act_id(mActId);
		pb.set_act_type(mActType);
	}
	void LoadFromPB(const OperateActItemInfo& pb)
	{
		mActId = pb.act_id();
		mActType = pb.act_type();
	}

	virtual bool IsFinishAll() { return false; }
};

//幸运转盘
class LucklyTurnTableAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(AllBuyCount);	//总共购买次数只和
	DECLARE_PROPERTY_UINT32(BuyCount);		//AllBuyCount % 5
	DECLARE_PROPERTY_BOOL(IsCanReceive);	//当前是否能接受奖励
	DECLARE_PROPERTY_UINT32(CanReceiveCount);	//能够领取奖励的次数
public:
	void SaveToPB(LucklyTurnTableActInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		pb.set_all_buy_count(mAllBuyCount);
		pb.set_buy_count(mBuyCount);
		pb.set_is_can_receive(mIsCanReceive);
		pb.set_can_receive_count(mCanReceiveCount);
	}
	void LoadFromPB(const LucklyTurnTableActInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		mAllBuyCount = pb.all_buy_count();
		mBuyCount = pb.buy_count();
		mIsCanReceive = pb.is_can_receive();
		mCanReceiveCount = pb.can_receive_count();
	}
};

//重复消费
class RepeatCostAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(LastReceiveRewardTime);	//上一次领取奖励的时间, s
	DECLARE_PROPERTY_UINT32(TodayReceiveCount);		//今天领取次数
	DECLARE_PROPERTY_UINT32(CostDismondCount);			//消费的砖石数目
	DECLARE_PROPERTY_BOOL(IsFinish);				//领取奖励
public:
	void SaveToPB(RepeatCostActInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		pb.set_last_receive_reward_time(mLastReceiveRewardTime);
		pb.set_today_receive_count(mTodayReceiveCount);
		pb.set_cost_diamond_count(mCostDismondCount);
		pb.set_is_finish(mIsFinish);
	}
	void LoadFromPB(const RepeatCostActInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		mLastReceiveRewardTime = pb.last_receive_reward_time();
		mTodayReceiveCount = pb.today_receive_count();
		mCostDismondCount = pb.cost_diamond_count();
		mIsFinish = pb.is_finish();
	}

	bool IsFinishAll()
	{
		if (mTodayReceiveCount >= 5)
		{
			return true;
		}

		return false;
	}
};

//累计消费
class AccumulationCostAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(CostDismondCount);			//消费的砖石数目
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem); //单个奖励领取情况
public:
	void SaveToPB(AccumulationCostActInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		pb.set_cost_diamond_count(mCostDismondCount);
		for(int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const AccumulationCostActInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		mCostDismondCount = pb.cost_diamond_count();
		for(int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}

	//是否所有奖励都领取完了
	bool IsFinishAll()
	{
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			if (mVecItem[i].GetIsReceiveReward() == false)
			{
				return false;
			}
		}
		return true;
	}
};

//等级礼包
class LevelGiftAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem); //单个奖励领取情况
public:
	void SaveToPB(LevelGiftActInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		for(int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const LevelGiftActInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		for(int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}

	bool IsFinishAll()
	{
		for(int i = 0; i < (int)mVecItem.size(); i++)
		{
			if (mVecItem[i].GetIsReceiveReward() == false)
			{
				return false;
			}
		}
		return true;
	}
};

//在线礼包
class OnlineGiftAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(LastTime); //上一次记录时间
	DECLARE_PROPERTY_UINT32(OnlineTime); //以及记下来的在线时间
	DECLARE_PROPERTY_UINT32(CurItemId);	//当前在线礼包奖励ID
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem); //单个奖励领取情况
public:
	//获得当前真正在线时间
	uint32_t GetAllOnlineTime()
	{
		uint32_t curTime = (uint32_t)g_GetGlobalServerTime()->UnixSec();
		
		if (curTime >= mLastTime)
		{
			return (mOnlineTime + curTime - mLastTime);
		}
		return 0;
	}

	//获得剩余时间
	uint32_t GetRemainingTime()
	{
		uint32_t allOnlineTime = GetAllOnlineTime();
		for(int i = 0; i < (int)mVecItem.size(); i++)
		{
			if (mVecItem[i].GetIsFinish() == false)
			{
				const WelfareConfigItem* pItemInfo = g_GetOperateWelfareMgr()->GetWelfareConfigItem(GetActId(), mVecItem[i].GetItemId());
				if (pItemInfo)
				{
					uint32_t maxOnlineTime = pItemInfo->GetActCondition().GetFaculty().GetMaxValue() * 60;
					if (maxOnlineTime > allOnlineTime)
					{
						return maxOnlineTime - allOnlineTime;
					}
					return 0;
				}
				break;
			}
		}
		return 0;
	}
public:
	void SaveToPB(OnlineGiftActInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		pb.set_last_time(mLastTime);
		pb.set_online_time(mOnlineTime);
		pb.set_remaining_time(GetRemainingTime());
		for(int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const OnlineGiftActInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		mLastTime = pb.last_time();
		mOnlineTime = pb.online_time();
		for(int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}

	bool IsFinishAll()
	{
		for(int i = 0; i < (int)mVecItem.size(); i++)
		{
			if (mVecItem[i].GetIsReceiveReward() == false)
			{
				return false;
			}
		}
		return true;
	}
};

//七日签到
class SevenDaySignAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_BOOL(TodaySign);		//今天是否签到
	DECLARE_PROPERTY_UINT32(LastSignTime);	//上一次时间
	DECLARE_PROPERTY_UINT32(ContinueSignDays); //单个奖励领取情况
public:
	void SaveToPB(SevenDaySignActInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		pb.set_today_sign(mTodaySign);
		pb.set_last_sign_time(mLastSignTime);
		pb.set_continue_sign_days(mContinueSignDays);
	}
	void LoadFromPB(const SevenDaySignActInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		mTodaySign = pb.today_sign();
		mLastSignTime = pb.last_sign_time();
		mContinueSignDays = pb.continue_sign_days();
	}
};

//每日活动
class EveryDaySignAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_INT32(LastSignTime);	//上一次时间
	DECLARE_PROPERTY_OBJECT(WelfareItem, MonthItem); //月完成度
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem); //单个奖励领取情况
public:
	void SaveToPB(EveryDaySignActInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		pb.set_last_sign_time(mLastSignTime);
		mMonthItem.SaveToPB(*pb.mutable_month_item());
		for(int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const EveryDaySignActInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		mLastSignTime = pb.last_sign_time();
		mMonthItem.LoadFromPB(pb.month_item());
		for(int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
};


//特殊掉落
class SpecialDropAct : public OperateActItem
{

};

class SpeicalExchangeItem
{
public:
	DECLARE_PROPERTY_UINT32(IndexId);	//特殊兑换表的索引ID
	DECLARE_PROPERTY_UINT32(ItemId);	//奖励表ID
	DECLARE_PROPERTY_UINT32(UsedCount);	//已经兑换的次数
};

typedef std::vector<SpeicalExchangeItem> VecSpeicalExchangeItem;

//特殊兑换
class SpecialExchangeAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(LastExchangeTime);	//上一次兑换时间
	DECLARE_PROPERTY_OBJECT(VecSpeicalExchangeItem, VecItem);	//特殊兑换所有单个兑换的情况
public:
	SpeicalExchangeItem* GetExchangeItem(uint32_t indexId)
	{
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			if (indexId == mVecItem[i].GetIndexId())
			{
				return &mVecItem[i];
			}
		}
		return nullptr;
	}
public:
	void SaveToPB(OperateExchangeRewardInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		pb.set_last_exchange_time(mLastExchangeTime);
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::OperateExchangeRewardItem* pItem = pb.add_reward_item();
			if (pItem)
			{
				pItem->set_index_id(mVecItem[i].GetIndexId());
				pItem->set_reward_id(mVecItem[i].GetItemId());
				pItem->set_used_count(mVecItem[i].GetUsedCount());
			}
		}
	}

	void LoadFromPB(const OperateExchangeRewardInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		mLastExchangeTime = pb.last_exchange_time();
		for (int i = 0; i < (int)pb.reward_item_size(); i++)
		{
			const ::ProtoBuf::OperateExchangeRewardItem& rewardItem = pb.reward_item(i);
			SpeicalExchangeItem item;
			item.SetIndexId(rewardItem.index_id());
			item.SetItemId(rewardItem.reward_id());
			item.SetUsedCount(rewardItem.used_count());
			mVecItem.push_back(item);
		}
	}
};

//单笔充值
class SinglePenRechargeAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem);//单个奖励领取情况
public:
public:
	void SaveToPB(SinglePenRechargeInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const SinglePenRechargeInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
}; 

//累计充值
class AccumulationRechargeAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(RechargeCount);	//累计充值的数目
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem);//单个奖励领取情况
public:
public:
	void SaveToPB(AccumulationRechargeInfo& pb)
	{
		pb.set_recharge_count(mRechargeCount);
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const AccumulationRechargeInfo& pb)
	{
		mRechargeCount = pb.recharge_count();
		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
};

//连冲豪礼
class ContinueRechargeAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT64(LastRechargeTime); //上一次充值的世界
	DECLARE_PROPERTY_UINT32(ContinueRechargeDays);//连续充值了几天
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem); //单个奖励领取情况
public:
	void SaveToPB(ContinueRechargeInfo& pb)
	{
		pb.set_last_recharge_time(mLastRechargeTime);
		pb.set_continue_recharge_days(mContinueRechargeDays);
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const ContinueRechargeInfo& pb)
	{
		mLastRechargeTime = pb.last_recharge_time();
		mContinueRechargeDays = pb.continue_recharge_days();
		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
};

//每日充值
class EveryDayRecharge : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(TodayRechargeCount);		//今天是否签到
	DECLARE_PROPERTY_UINT32(LastRechargeTime);	//上一次时间
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem); //单个奖励领取情况
public:
	void SaveToPB(EveryDayRechargeInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		pb.set_today_recharge_count(mTodayRechargeCount);
		pb.set_last_recharge_time(mLastRechargeTime);
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const EveryDayRechargeInfo& pb)
	{
		mTodayRechargeCount = pb.today_recharge_count();
		mLastRechargeTime = pb.last_recharge_time();
		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
};

//超值礼包
class SuperValueRechargeGift : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(LastRechargeTime);	//上一次时间
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem);//单个奖励领取情况
public:
	void SaveToPB(SuperValueRechargeGiftInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		pb.set_last_recharge_time(mLastRechargeTime);
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const SuperValueRechargeGiftInfo& pb)
	{
		mLastRechargeTime = pb.last_recharge_time();
		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
};

//首冲礼包
class FirstRechargeGift : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(AccumulationRecharge);	//累计充值数目
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem);//单个奖励领取情况
public:
	void SaveToPB(FirstRechargeGiftInfo& pb)
	{
		pb.set_accumulation_recharge(mAccumulationRecharge);
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const FirstRechargeGiftInfo& pb)
	{
		mAccumulationRecharge = pb.accumulation_recharge();
		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
};

//限定礼包
class LimitsRechargeGift : public OperateActItem
{
public:
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem);//单个奖励领取情况
public:
	void SaveToPB(LimitsRechargeGiftInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const LimitsRechargeGiftInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
};

//今日限定礼包
class TodayLimitsGift : public OperateActItem
{
public:
	DECLARE_PROPERTY_UINT32(CurItemId);	//今日限定当前礼包奖励ID
	DECLARE_PROPERTY_UINT32(CreateAccountTime);	//创建账号时间
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem);//单个奖励领取情况
public:
	void SaveToPB(TodayLimitsGiftInfo& pb)
	{
		pb.set_cur_item_id(mCurItemId);
		pb.set_create_account_time(mCreateAccountTime);
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
	}
	void LoadFromPB(const TodayLimitsGiftInfo& pb)
	{
		mCurItemId = pb.cur_item_id();
		mCreateAccountTime = pb.create_account_time();
		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
};

//世界大师
class WorldMasterGift : public OperateActItem
{
public:
	enum
	{
		First_STONE_INLAY = 1,	//第一周
		Second_EQUIP_REFINE = 2,	//第二周
		Third_UNION_FIGHT_UPGRADE = 3, //第三周
	};
public:
	DECLARE_PROPERTY_UINT32(CreateAccountTime);		//创建账号
	DECLARE_PROPERTY_UINT32(UnionFightMaxLevel);	//当前公会战斗最大等级
	DECLARE_PROPERTY_UINT32(EquipRefineMaxLevel);	//当前玩家身上装备精炼之和最大等级
	DECLARE_PROPERTY_UINT32(StoneInlaysMaxLevel);	//当前玩家身上嵌套宝石之和最大等级
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem);//单个奖励领取情况
public:
	//判断当前是否在建立账号的第几周
	bool IsInWeek(int week)
	{
		uint32_t curTime = (uint32_t)g_GetGlobalServerTime()->UnixSec();
		if (curTime >= (mCreateAccountTime + 86400 * 7 * (week-1)) && curTime <= (mCreateAccountTime + 86400 * 7 * week))
		{
			return true;
		}
		return false;
	}
public:
	void SaveToPB(WorldMasterGiftInfo& pb)
	{
		pb.set_union_fight_max_level(mUnionFightMaxLevel);
		pb.set_equip_refine_max_level(mEquipRefineMaxLevel);
		pb.set_stone_inlays_max_level(mStoneInlaysMaxLevel);
		pb.set_create_account_time(mCreateAccountTime);
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
		if (IsInWeek(First_STONE_INLAY))
		{
			pb.set_week(First_STONE_INLAY);
		}
		else if (IsInWeek(Second_EQUIP_REFINE))
		{
			pb.set_week(Second_EQUIP_REFINE);
		}
		else if (IsInWeek(Third_UNION_FIGHT_UPGRADE))
		{
			pb.set_week(Third_UNION_FIGHT_UPGRADE);
		}

		uint32_t day = (uint32_t)g_GetGlobalServerTime()->UnixSec() - mCreateAccountTime;
		day = 7 * pb.week() - (day / 86400);
		pb.set_day(day);
	}
	void LoadFromPB(const WorldMasterGiftInfo& pb)
	{
		mStoneInlaysMaxLevel = pb.stone_inlays_max_level();
		mEquipRefineMaxLevel = pb.equip_refine_max_level();
		mUnionFightMaxLevel = pb.union_fight_max_level();
		mCreateAccountTime = pb.create_account_time();
		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
};

//积分转盘积分信息
class PointTurnPointInfo
{
public:
	DECLARE_PROPERTY_UINT32(Id);	//积分转盘积分表ID
	DECLARE_PROPERTY_UINT32(Type);	//任务类型
	DECLARE_PROPERTY_UINT32(Param);	//达成参数
	DECLARE_PROPERTY_UINT32(CurParam);	//当前已经达成的参数
	DECLARE_PROPERTY_UINT32(Point);	//积分点
	DECLARE_PROPERTY_UINT32(Times);	//次数
	DECLARE_PROPERTY_UINT32(CurTime);	//当前以及做到了的次数
	DECLARE_PROPERTY_BOOL(IsFinish)	//是否完成
public:
	void SaveToPB(PointTurnPointData& pb)
	{
		pb.set_id(mId);
		pb.set_type(mType);
		pb.set_param(mParam);
		pb.set_cur_param(mCurParam);
		pb.set_point(mPoint);
		pb.set_times(mTimes);
		pb.set_cur_time(mCurTime);
		pb.set_is_finish(mIsFinish);
	}
public:
	void LoadFromPB(const PointTurnPointData& pb)
	{
		mId = pb.id();
		mType = pb.type();
		mParam = pb.param();
		mCurParam = pb.cur_param();
		mPoint = pb.point();
		mTimes = pb.times();
		mCurTime = pb.cur_time();
		mIsFinish = pb.is_finish();
	}
};

typedef std::vector<PointTurnPointInfo> VecPointInfo;

//积分转盘
class PointTurnTableGift : public OperateActItem
{
public:
	enum
	{	//0, 4, 8, 是绑砖所在位置，固定死的
		INDEX_1 = 0,
		INDEX_2 = 4,
		INDEX_3 = 8,
	};

	///任务类型
	enum
	{
		EVERY_DAY_SIGN = 1,			//每日签到
		KILL_MONSTER_COUNT = 2,		//击杀数量	
		EVERY_DAY_ACTIVITY = 3,		//每日活跃度
		EVERY_DAY_RECHARGE = 4,		//每日充值任务金额
		EQUIP_ZHIZHAO = 5,		//装备制造
		FENG_XIANG = 6,		//分享
		WORLD_SAY = 7,		//世界发言
		COST_DIAMOND = 8,		//砖石消费
	};
public:
	DECLARE_PROPERTY_UINT32(RemainPoint);			//剩余积分
	DECLARE_PROPERTY_UINT32(CurPoint);			//当前积分
	DECLARE_PROPERTY_UINT32(DrawRewardCount);			//可以抽奖的次数
	DECLARE_PROPERTY_UINT32(BindDiamond);			//砖石/绑砖数量
	DECLARE_PROPERTY_UINT32(LastTime);			//上次记录时间
	DECLARE_PROPERTY_OBJECT(VecWelfareItem, VecItem);			//奖励ID，只有一个
	DECLARE_PROPERTY_OBJECT(VecPointInfo, VecPointInfo);			//积分转盘积分类型的信息
public:
	//增加加分
	void AddPoint(uint32_t point);
	//计算积分
	bool CountPointEvent(int event, int count)
	{
		for (int i = 0; i < (int)mVecPointInfo.size(); i++)
		{
			if (mVecPointInfo[i].GetType() == (uint32_t)event)
			{
				if (mVecPointInfo[i].GetIsFinish() == true) return false;

				mVecPointInfo[i].SetCurParam(mVecPointInfo[i].GetCurParam() + count);
				if (mVecPointInfo[i].GetCurParam() >= mVecPointInfo[i].GetParam())
				{
					AddPoint(mVecPointInfo[i].GetPoint());
					mVecPointInfo[i].SetCurTime(mVecPointInfo[i].GetCurTime() + 1);
					mVecPointInfo[i].SetCurParam(mVecPointInfo[i].GetCurParam() % mVecPointInfo[i].GetParam());
					if (mVecPointInfo[i].GetCurTime() >= mVecPointInfo[i].GetTimes())
					{
						mVecPointInfo[i].SetIsFinish(true);
					}
					return true;
				}
				return false;
			}
		}
		return false;
	}

	void SaveToPB(PointTurnTableInfo& pb)
	{
		pb.set_last_time(mLastTime);
		pb.set_remain_point(mRemainPoint);
		pb.set_cur_point(mCurPoint);
		pb.set_draw_reward_count(mDrawRewardCount);
		pb.set_bind_diamond(mBindDiamond);
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			::ProtoBuf::WelfareItemInfo* pInfo = pb.add_item_info();
			mVecItem[i].SaveToPB(*pInfo);
		}
		for (int i = 0; i < (int)mVecPointInfo.size(); i++)
		{
			mVecPointInfo[i].SaveToPB(*pb.add_data());
		}
	}
	void LoadFromPB(const PointTurnTableInfo& pb)
	{
		mLastTime = pb.last_time();
		mRemainPoint = pb.remain_point();
		mCurPoint = pb.cur_point();
		mDrawRewardCount = pb.draw_reward_count();
		mBindDiamond = pb.bind_diamond();

		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			WelfareItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}

		for (int i = 0; i < (int)pb.data_size(); i++)
		{
			PointTurnPointInfo info;
			info.LoadFromPB(pb.data(i));
			mVecPointInfo.push_back(info);
		}
	}
};

class GrowthFundLevelItem
{
public:
	DECLARE_PROPERTY_UINT32(Level);	//成长基金等级
	DECLARE_PROPERTY_BOOL(LevelFinish); //是否完成
	DECLARE_PROPERTY_BOOL(IsReiveReward); //是否领取奖励
public:
	void SaveToPB(GrowthFundLevelItemInfo& pb)
	{
		pb.set_level(mLevel);
		pb.set_level_finish(mLevelFinish);//判断等级是否达到
		pb.set_is_receive_reward(mIsReiveReward);
	}
	void LoadFromPB(const GrowthFundLevelItemInfo& pb)
	{
		mLevel = pb.level();
		mLevelFinish = pb.level_finish();
		mIsReiveReward = pb.is_receive_reward();
	}
};

typedef std::vector<GrowthFundLevelItem> VecGrowthFundLevelItem;

class GrowthFundItem : public WelfareItem
{
public:
	GrowthFundItem() { }
	~GrowthFundItem() { }
public:
	DECLARE_PROPERTY_OBJECT(VecGrowthFundLevelItem, VecItem);	//	
public:
	void SaveToPB(GrowthFundItemInfo& pb)
	{
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			mVecItem[i].SaveToPB(*pb.add_level_item());
		}
		WelfareItem::SaveToPB(*pb.mutable_item_info());
	}
	void LoadFromPB(const GrowthFundItemInfo& pb)
	{
		for (int i = 0; i < (int)pb.level_item_size(); i++)
		{
			GrowthFundLevelItem item;
			item.LoadFromPB(pb.level_item(i));
			mVecItem.push_back(item);
		}
		WelfareItem::LoadFromPB(pb.item_info());
	}
};

typedef std::vector<GrowthFundItem> VecGrowthFundItem;

//成长基金
class GrowthFundAct : public OperateActItem
{
public:
	DECLARE_PROPERTY_OBJECT(VecGrowthFundItem, VecItem);
public:
	void SaveToPB(GrowthFundInfo& pb)
	{
		OperateActItem::SaveToPB(*pb.mutable_act_info());
		for (int i = 0; i < (int)mVecItem.size(); i++)
		{
			mVecItem[i].SaveToPB(*pb.add_item_info());
		}
	}
	void LoadFromPB(const GrowthFundInfo& pb)
	{
		OperateActItem::LoadFromPB(pb.act_info());
		for (int i = 0; i < (int)pb.item_info_size(); i++)
		{
			GrowthFundItem item;
			item.LoadFromPB(pb.item_info(i));
			mVecItem.push_back(item);
		}
	}
};


//定时器, 0点定时器
enum OperateWelfareTimer
{
	OPERATE_WELFARE_ZERO_TIMER = 1, //0点定时器
	OPERATE_WELFARE_ONLINE_TIMER = 2, //在线时间定时器
};

class OperateWelfarePart : public Part
{
public:
	OperateWelfarePart();
	virtual ~OperateWelfarePart();
public:
	bool Init(Creature *pMaster, uint32_t partType, CharacterDBInfo* pCharacterDB = nullptr);
	bool UnInit();
	void OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen);
	bool OnMsgHandle(uint32_t cmd, const char* data, uint32_t len);
	void OnTimer(uint32_t nTimerId);
	bool SaveDB(CharacterDBInfo& characterDB);
	virtual void Offline();
	virtual void SynLogout();
	virtual void BeforeChangeLogic();
public:
	
	void InitGrowthFund();						//初始化成长基金
	void InitSinglePenRecharge();						//初始化单笔充值
	void InitAccumulationRecharge();						//初始化累积充值
	void InitContinueRecharge();						//初始化持续充值

	void InitOnlineGiftTimer();						//初始化在线礼包
	void InitNewDay();						//初始化新的一天
	void InitOnlineGiftNewDay();						//初始化在线礼包新的一天
	void InitSevenDaySign();						//初始化7日签到
	void CountOnlineGiftTime();						//计算当前在线礼包的在线时间
	void CountSevenDaySign();						//计算七日签到
	void InitSevenDayNewDay();						//初始化七日签到新的一天
	void InitEveryDaySign();						//初始化每日签到
	void CountEveryDaySign();						//计算每日签到
	void InitEveryDayNewDay();						//初始化每日签到新的一天
	void CountEveryDayMonthFinish();						//计算每日签到月的完成度
	void NotifyOperateActIds();						//通知客户端哪些运营活动项目还在
	void InitRepeatCostNewDay();						//初始化重复消费新的一天
	void InitSpecialDropDay();						//初始化特殊掉落
	void InitSpecialExchangeDay();						//初始化特殊兑换
	void InitEveryDayRecharge();						//初始化每日对话
	void InitSuperValueGift();						//初始化超值礼包
	void InitFirstRechargeGift();						//初始化首冲礼包
	void InitLimitsRechargeGift();						//初始化限定礼包
	void InitTodayLimitsGift();						//初始化今日限定
	void InitTodayLimitsNewDay();						//初始化今日限定新的一天
	void InitLucklyTurn();						//初始化幸运转盘
	void InitWorldMasterGiftInfo();						//初始化世界大师
	void InitPointTurnInfo();						//初始化积分转盘
	void InitPointTurnInfoNewDay();						//初始化积分转盘新的一天
	bool HasSpecialExchangeCanExchange();						//是否有特殊兑换需要兑换
public:
	void OnHandleGetWelfareReward(uint32_t cmd, const char* data, uint32_t len);				//运营活动获得奖励
	void OnHandleZhuanPanChouJiang(uint32_t cmd, const char* data, uint32_t len);				//幸运转盘抽奖
	void OnHandleGetRepeatedCostInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetAccumulationCostInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetLevelGiftInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetOnlineGiftInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetSevenDaySignInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetEveryDaySignInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetOperateActIds(uint32_t cmd, const char* data, uint32_t len);			//获得运营获得ID
	void OnHandleHasSign(uint32_t cmd, const char* data, uint32_t len);						//是否需要签到
	void OnHandleSpecialExchangeReward(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetSpecialExchangeInfo(uint32_t cmd, const char* data, uint32_t len);

	void OnHandleSinglePenRechargeInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleAccumulationRechargeInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleContinueRechargeInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGrowthFundInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleEveryDayRechargeInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleSuperValueGiftInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleFirstRechargeGiftInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleLimitRechargeGiftInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleTodayLimitGiftInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetLucklyTurnInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetWorldMasterInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetPointTurnInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandlePointTurnChouJiang(uint32_t cmd, const char* data, uint32_t len);			//积分转盘抽检
	void OnHandlePointTurnGetBindDiamond(uint32_t cmd, const char* data, uint32_t len);		//积分转盘获得砖石
	void OnHandleSharePictureSuccess(uint32_t cmd, const char* data, uint32_t len);			//分享成功
public:
	bool IsOpenLevelWelfareAction();	//达到运营活动开启等级
	bool IsOpenLevelWelfare();			//达到福利开启等级
	bool IsOpenLevelSevenTarget();		//达到7日目标开启等级
	bool IsOpenLevelFirstRecharge();	//达到首冲开启等级
	bool IsOpenLevelDayLimit();			//达到今日限定开启等级
public:
	void InitConfig();
	void InitConfigFromDB(const ::ProtoBuf::CharacterDBOperateWelfareInfo& DBWelfareInfo);
public:
	DECLARE_PROPERTY_OBJECT(LucklyTurnTableAct, LucklyTurnTableAct);	//幸运转盘
	DECLARE_PROPERTY_OBJECT(RepeatCostAct, RepeatCostAct);			//重复消费
	DECLARE_PROPERTY_OBJECT(AccumulationCostAct, AccumulationCostAct);	//累计消费
	DECLARE_PROPERTY_OBJECT(LevelGiftAct, LevelGiftAct);			//等级礼包
	DECLARE_PROPERTY_OBJECT(OnlineGiftAct, OnlineGiftAct);			//在线礼包
	DECLARE_PROPERTY_OBJECT(SevenDaySignAct, SevenDaySignAct);		//七日签到
	DECLARE_PROPERTY_OBJECT(EveryDaySignAct, EveryDaySignAct);		//每日活动
	DECLARE_PROPERTY_OBJECT(SpecialDropAct, SpecialDropAct);		//特殊掉落
	DECLARE_PROPERTY_OBJECT(SpecialExchangeAct, SpecialExchangeAct);//特殊兑换
	DECLARE_PROPERTY_OBJECT(SinglePenRechargeAct, SinglePenRechargeAct);	//单笔充值
	DECLARE_PROPERTY_OBJECT(AccumulationRechargeAct, AccumulationRechargeAct);	//累计充值
	DECLARE_PROPERTY_OBJECT(ContinueRechargeAct, ContinueRechargeAct);	//连冲豪礼
	DECLARE_PROPERTY_OBJECT(GrowthFundAct, GrowthFundAct);			//成长基金
	DECLARE_PROPERTY_OBJECT(EveryDayRecharge, EveryDayRecharge);	//每日充值
	DECLARE_PROPERTY_OBJECT(SuperValueRechargeGift, SuperValueRechargeGift); //超值礼包
	DECLARE_PROPERTY_OBJECT(FirstRechargeGift, FirstRechargeGift);	//首冲
	DECLARE_PROPERTY_OBJECT(LimitsRechargeGift, LimitsRechargeGift); //限定礼包
	DECLARE_PROPERTY_OBJECT(TodayLimitsGift, TodayLimitsGift);	//今日限定
	DECLARE_PROPERTY_OBJECT(WorldMasterGift, WorldMasterGift); //世界大师
	DECLARE_PROPERTY_OBJECT(PointTurnTableGift, PointTurnTableGift); //积分转盘
	//七日目标活动
public:
	void Goals7DaysInit(CharacterDBInfo* pCharacterDB);
	void Goals7DaysSave(::ProtoBuf::CharacterDBOperateWelfareInfo* welfare);
	Goals7DaysData* Goals7DaysMut(Goals7DayType gType, uint32_t targetId, uint32_t goalsId);
	void Goals7DaysUpdate(Goals7DayType gType, Goals7DayUpdateType uType, Goals7DaySubType subType, int32_t value, int32_t obj = 0);
	void Goals7DaysDailyUpdate();
	bool Goals7DaysNeedStatics() { return _needStatics; }
	//客户端交互
	void OnHandleGetGoals7DaysInfo(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetGoals7DaysAwards(uint32_t cmd, const char* data, uint32_t len);
	void OnHandleGetGoals7DaysProAwards(uint32_t cmd, const char* data, uint32_t len);

	bool DoSale(uint32_t saleId);		//促销活动购买
	bool Display();						//前端是否弹窗

	bool _needStatics;
	bool _display;				//通知前端弹窗(根据今天是否能领取登录奖励)
	int32_t _curDays;			//创角天数
	uint64_t _goalsCreateTime;	//创角时间
	uint32_t _progress;			//当前七日目标活动进度
	SET_UINT32 _progressSet;	//已领取的进度奖励
	MAP_ALL_GOALS7DAYS _allGoal7DaysMap;
	
	
};





